import {Component, OnInit, ViewChild} from '@angular/core';
import {HttpService} from "../../../lib/service/http.service";
import {adSystemApiUrl, tuikeSystemUrl} from "../../../../config/api";
import {ModalDirective} from "ngx-bootstrap";
import {Headers, RequestOptions} from "@angular/http";

@Component({
  selector: 'app-list',
  templateUrl: './list.component.html',
  styleUrls: ['./list.component.scss']
})
export class ListComponent implements OnInit {


  searchData = '';
  invitationList = [];
  userName: string;
  psd: string;
  password_again: string;
  operateId: number;  // 当前正在操作的账号ID
  resetPsd: string;
  resetPsdAgain: string;
  status = '';

  type: string;
  message: string;
  isShow = true;
  totalItems: number;
  currentPage: number = 1;
  itemsPerPage: number = 20;
  _isSpinning = false;

  @ViewChild('addClientModal') public addClientModal: ModalDirective;
  @ViewChild('resetPsdModal') public resetPsdModal: ModalDirective;

  constructor(private httpService: HttpService) {
  }

  ngOnInit() {
    this.getInvitationList(1);
  }

  //重置
  reset() {
    this.searchData = '';
    this.getInvitationList(1);
  }

  //搜索
  search() {
    this.getInvitationList(1);
  }

  // 获取客户列表

  getInvitationList(page: number) {
    this._isSpinning = true;
    let searchType = '';
    if (this.searchData.length === 28) {
      searchType = 'tuike_openid';
    } else {
      searchType = 'code';
    }

    let url = `${tuikeSystemUrl}/api/v1/admin/invite_code/code_list?page=${page}&pageSize=${this.itemsPerPage}&${searchType}=${this.searchData}&status=${this.status}`;
    return this.httpService.httpPostAuth(url,{}).subscribe(res => {
      if (res.code == 200) {
        this._isSpinning = false;
        this.invitationList = res.data.data;
        this.totalItems = res.data.total;
      }
    })
  }


  pageChanged(event: any): void {
    window.scrollTo(0, 0);
    this.getInvitationList(event.page);
  }
}
